package home

import (
	"github.com/gin-gonic/gin"

	"github.com/zeromicro/go-zero/tools/goctl/example/api/internal/domain"
	"github.com/zeromicro/go-zero/tools/goctl/example/api/internal/logic"
	"github.com/zeromicro/go-zero/tools/goctl/example/api/internal/svc"
	"github.com/zeromicro/go-zero/tools/goctl/example/api/pkg/httpx"
)

type Prompts struct {
	svcCtx  *svc.ServiceContext
	prompts logic.Prompts
}

func NewPrompts(svcCtx *svc.ServiceContext, prompts logic.Prompts) *Prompts {
	return &Prompts{
		svcCtx:  svcCtx,
		prompts: prompts,
	}
}

func (h *Prompts) InitRegister(engine *gin.Engine) {
	g := engine.Group("v1/prompts")
	g.GET("", h.List)
	g.GET("/:id", h.Info)
	g.POST("/edit", h.Edit)
}

// 获取所有的prompt列表
func (h *Prompts) List(ctx *gin.Context) {
	res, err := h.prompts.List(ctx.Request.Context())
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}

// prompts的详情
func (h *Prompts) Info(ctx *gin.Context) {
	var req domain.IdReq
	if err := httpx.BindAndValidate(ctx, &req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}

	res, err := h.prompts.Info(ctx.Request.Context(), &req)
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}

// 修改prompts的详情
func (h *Prompts) Edit(ctx *gin.Context) {
	var req domain.Prompts
	if err := httpx.BindAndValidate(ctx, &req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}

	res, err := h.prompts.Edit(ctx.Request.Context(), &req)
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}
